package com.kgc.warehouse.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kgc.warehouse.system.common.CodeMsg;
import com.kgc.warehouse.system.common.ResponseResult;
import com.kgc.warehouse.system.entity.RolePermission;
import com.kgc.warehouse.system.mapper.RolePermissionMapper;
import com.kgc.warehouse.system.mapper.UserRoleMapper;
import com.kgc.warehouse.system.service.RolePermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
* @author main
* @description 针对表【sys_role_permission】的数据库操作Service实现
* @createDate 2023-02-03 20:07:42
*/
@Service
public class RolePermissionServiceImpl extends ServiceImpl<RolePermissionMapper, RolePermission>
    implements RolePermissionService{

    @Autowired(required = false)
    private RolePermissionMapper rolePermissionMapper;


    @Override
    public ResponseResult addRolePermission(Integer roleId, String permissionIds) {

        //先清除

        LambdaQueryWrapper<RolePermission> rolePermissionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        rolePermissionLambdaQueryWrapper.in(RolePermission::getRid,roleId);
        rolePermissionMapper.delete(rolePermissionLambdaQueryWrapper);
        //在添加
        String[] split = permissionIds.split(",");
        ArrayList<Integer> integers = new ArrayList<>();
        for (String s : split) {
            integers.add(Integer.valueOf(s));
        }
        integers.forEach(integer -> {
            RolePermission rolePermission = new RolePermission();
            rolePermission.setPid(integer);
            rolePermission.setRid(roleId);
            rolePermissionMapper.insert(rolePermission);
        });

        return new ResponseResult(CodeMsg.SUCCESS);
    }
}




